package com.gx.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.gx.common.constants.XxlApiConstants;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * 操作xxl-job的工具类
 *
 * @author Promsing(张有博)
 * @version 1.0.0
 * @since 2022/3/30 - 15:08
 */
@Component
public class XxlJobUtil {
    /**
     * 新增/编辑任务
     *
     * @param requestInfo 请求参数
     */
    public static JSONObject addJob(JSONObject requestInfo) {

        String targetUrl = XxlApiConstants.ADMIN_ADDRESSES + XxlApiConstants.ADD;
        //2.获得cookie
        String cookie = XxlJobUtil.login();
        HttpRequest request = HttpUtil.createPost(targetUrl);
        request.header("Cookie", cookie);
        request.form(requestInfo);
        HttpResponse execute = request.execute();
        if (execute.isOk()) {
            JSONObject res = JSONObject.parseObject(execute.body());
            //修改任务参数 把id放入
            // 执行器主键ID
            requestInfo.put("executorParam", "JobId=" + res.get("content"));
            requestInfo.put("id", Integer.valueOf(res.get("content").toString()));
            XxlJobUtil.updateJob(requestInfo);
            return res;
        }
        throw new RuntimeException("任务新增失败!");
    }

    /**
     * 修改任务
     *
     * @param requestInfo 参数
     */
    public static JSONObject updateJob(JSONObject requestInfo) {
        String targetUrl = XxlApiConstants.ADMIN_ADDRESSES + XxlApiConstants.UPDATE;
        HttpRequest request = HttpUtil.createPost(targetUrl);
        request.form(requestInfo);
        HttpResponse response = request.execute();
        if (response.isOk()) {
            return JSONObject.parseObject(response.body());
        }
        return null;
    }

    /**
     * 开始任务
     *
     * @param id 任务id
     */
    public static JSONObject startJob(int id) {
        //1.拼接URL
        String targetUrl = XxlApiConstants.ADMIN_ADDRESSES + XxlApiConstants.START;
        //2.获得cookie
        String cookie = XxlJobUtil.login();
        //3.封装请求参数
        JSONObject requestInfo = new JSONObject();
        requestInfo.put("id", id);
        HttpRequest request = HttpUtil.createPost(targetUrl);
        request.header("Cookie", cookie);
        request.form(requestInfo);
        HttpResponse response = request.execute();
        return JSONObject.parseObject(response.body());
    }

    /**
     * 执行一次
     *
     * @param id 任务id
     */
    public static JSONObject trigger(int id) {
        //1.拼接URL
        String targetUrl = XxlApiConstants.ADMIN_ADDRESSES + XxlApiConstants.TRIGGER;
        //2.获得cookie
        String cookie = XxlJobUtil.login();
        //3.封装请求参数
        JSONObject requestInfo = new JSONObject();
        requestInfo.put("id", id);
        HttpRequest request = HttpUtil.createPost(targetUrl);
        request.header("Cookie", cookie);
        request.form(requestInfo);
        HttpResponse response = request.execute();
        return JSONObject.parseObject(response.body());
    }

    /**
     * 删除任务
     *
     * @param id 任务id
     */
    public static JSONObject deleteJob(int id) {
        //1.拼接URL
        String targetUrl = XxlApiConstants.ADMIN_ADDRESSES + XxlApiConstants.REMOVE;
        //2.获得cookie
        String cookie = XxlJobUtil.login();
        //3.封装请求参数
        JSONObject requestInfo = new JSONObject();
        requestInfo.put("id", id);
        HttpRequest request = HttpUtil.createPost(targetUrl);
        request.header("Cookie", cookie);
        request.form(requestInfo);
        HttpResponse response = request.execute();
        return JSONObject.parseObject(response.body());
    }


    /**
     * 停止任务
     *
     * @param id
     */
    public static JSONObject stopJob(int id) {
        //1.拼接URL
        String targetUrl = XxlApiConstants.ADMIN_ADDRESSES + XxlApiConstants.STOP;
        //2.获得cookie
        String cookie = XxlJobUtil.login();
        //3.封装请求参数
        JSONObject requestInfo = new JSONObject();
        requestInfo.put("id", id);
        HttpRequest request = HttpUtil.createPost(targetUrl);
        request.header("Cookie", cookie);
        request.form(requestInfo);
        HttpResponse response = request.execute();
        return JSONObject.parseObject(response.body());
    }

    /**
     * 登录xxl-job
     *
     * @return 返回cookie
     */
    public static String login() {
        //1.拼接URL
        String targetUrl = XxlApiConstants.ADMIN_ADDRESSES + XxlApiConstants.LOGIN;
        //2.封装参数
        HashMap<String, Object> requestInfo = new HashMap<>();
        requestInfo.put("userName", "admin");
        requestInfo.put("password", "123456");
        HttpRequest request = HttpUtil.createPost(targetUrl);
        request.form(requestInfo);
        HttpResponse execute = request.execute();
        return execute.header("Set-Cookie");
    }
}